using DesktopWidgetApp.Core.Data;
using DesktopWidgetApp.Shared;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DesktopWidgetApp.Core.Services
{
    /// <summary>
    /// 状态服务类
    /// 提供状态管理相关的业务逻辑
    /// </summary>
    public class StatusService
    {
        private readonly IStatusRepository _statusRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="statusRepository">状态仓储</param>
        public StatusService(IStatusRepository statusRepository)
        {
            _statusRepository = statusRepository;
        }

        /// <summary>
        /// 更新用户状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="statusType">状态类型</param>
        /// <param name="customText">自定义状态文本</param>
        /// <param name="color">状态颜色</param>
        /// <param name="isDoNotDisturb">是否免打扰</param>
        /// <returns>更新后的状态</returns>
        public async Task<Status> UpdateUserStatusAsync(Guid userId, StatusType statusType, string customText = null, string color = null, bool isDoNotDisturb = false)
        {
            // 先结束用户当前的状态
            var currentStatus = await _statusRepository.GetCurrentStatusByUserIdAsync(userId);
            if (currentStatus != null && !currentStatus.EndTime.HasValue)
            {
                await _statusRepository.EndCurrentStatusAsync(userId, DateTime.Now);
            }

            // 创建新状态
            var newStatus = new Status
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                Type = statusType,
                CustomText = customText,
                Color = color,
                StartTime = DateTime.Now,
                EndTime = null,
                IsDoNotDisturb = isDoNotDisturb,
                LastUpdated = DateTime.Now,
                HasBeenSynced = false
            };

            await _statusRepository.AddAsync(newStatus);
            return newStatus;
        }

        /// <summary>
        /// 获取用户当前状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>当前状态</returns>
        public async Task<Status> GetUserCurrentStatusAsync(Guid userId)
        {
            return await _statusRepository.GetCurrentStatusByUserIdAsync(userId);
        }

        /// <summary>
        /// 获取用户状态历史
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>状态历史列表</returns>
        public async Task<IEnumerable<Status>> GetUserStatusHistoryAsync(Guid userId)
        {
            return await _statusRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 结束用户当前状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        public async Task EndUserCurrentStatusAsync(Guid userId)
        {
            await _statusRepository.EndCurrentStatusAsync(userId, DateTime.Now);
        }

        /// <summary>
        /// 获取处于免打扰状态的用户ID列表
        /// </summary>
        /// <returns>免打扰用户ID列表</returns>
        public async Task<IEnumerable<Guid>> GetDoNotDisturbUserIdsAsync()
        {
            return await _statusRepository.GetDoNotDisturbUserIdsAsync();
        }

        /// <summary>
        /// 检查用户是否处于免打扰状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>是否免打扰</returns>
        public async Task<bool> IsUserInDoNotDisturbAsync(Guid userId)
        {
            var currentStatus = await _statusRepository.GetCurrentStatusByUserIdAsync(userId);
            return currentStatus != null && currentStatus.IsDoNotDisturb && !currentStatus.EndTime.HasValue;
        }

        /// <summary>
        /// 获取指定类型的所有状态
        /// </summary>
        /// <param name="statusType">状态类型</param>
        /// <returns>指定类型的状态列表</returns>
        public async Task<IEnumerable<Status>> GetStatusesByTypeAsync(StatusType statusType)
        {
            return await _statusRepository.GetByStatusTypeAsync(statusType);
        }
    }
}